package com.lyl.serviceedu.controller;


import com.lyl.commonutils.R;
import com.lyl.serviceedu.entity.Subject;
import com.lyl.serviceedu.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-08
 */
@RestController
@RequestMapping("/serviceedu/subject")
public class SubjectController {
    @Autowired
    SubjectService subjectService;

    @PostMapping
    public R add(@RequestBody Subject subject){
     boolean isOk=   subjectService.addSubject(subject);
     if(isOk){
         return R.ok().message("添加成功").data("subject",subject);
     }else{
         return  R.error().message("添加失败");
     }

    }

    /**
     * 批量上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R importExcelSubject(@RequestParam("file") MultipartFile file){
        subjectService.batchImport(file);
        return  R.ok().message("上传成功");
    }

    @GetMapping()
    public R getSubjectTypeInfo(){

        return  R.ok().data("tree",subjectService.getAllSubject(null));
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id){
     boolean isOk=   subjectService.deleteById(id);
     if(isOk){
         return  R.ok().message("删除成功");
     }else{
         return  R.error().message("删除失败");
     }

    }
}

